package logic.ships.frigate;

import java.util.ArrayList;
import java.util.Collection;

import networking.messages.createMessages.ShipType;

import com.jme.scene.Node;

import logic.common.missions.Mission;
import logic.common.missions.MissionFactory;
import logic.common.team.Team;
import logic.nodes.TeamNode;
import logic.nodes.collision.DummyType;
import logic.ships.MissionShip;
import logic.ships.autoWeaponShip.AutoWeaponController;
import logic.ships.autoWeaponShip.AutoWeaponShip;
import logic.ships.autoWeaponShip.AutoWeaponTPController;
import logic.ships.autoWeaponShip.AutoWeaponShipProperties;
import logic.ships.moveableShip.MovableShip;
import logic.weapons.Weapon;
import logic.weapons.energyWeapons.EnergyWeapon;
import logic.weapons.projectileWeapons.missile.MissileWeapon;

public class Frigate extends MovableShip implements AutoWeaponShip, MissionShip {
	
	private static final long serialVersionUID = 1L;
	
	private FrigateController frigateController;
	
	private FrigateBlockFinder energyBlockFinder, missileBlockFinder;
	
	private TeamNode currentTarget;
	
	public Frigate(String modelPath, String title, Node model, AutoWeaponShipProperties properties, Team team) {
		this(modelPath, title, model, properties, team, DummyType.None);
	}
	
	public Frigate(String modelPath, String title, Node model, AutoWeaponShipProperties properties, Team team, 
			DummyType dummyType) {
		super(modelPath, title, model, properties, team, dummyType);
		
		if(dummyType == DummyType.None) createMoveForwardController();
	}
	
	@Override
	protected void applyDummyType() {
		super.applyDummyType();
		
		if(dummyType == DummyType.None) {
			frigateController = new FrigateController(this);
			addController(frigateController);
			
			initBlockFinders();
		}
	}
	
	private void initBlockFinders() {
		energyBlockFinder = new FrigateBlockFinder(this) {
			@Override
			public float getRange() { return getAutoWeaponShipProps().getEnergyRange(); }
		};
		
		missileBlockFinder = new FrigateBlockFinder(this) {
			@Override
			public float getRange() { return getAutoWeaponShipProps().getMissileRange(); }
		};
	}
	
	@Override
	public void addWeapon(Weapon newWeapon) {
		super.addWeapon(newWeapon);
		
		FrigateBlockFinder blockFinder = null;
		if(newWeapon instanceof EnergyWeapon) blockFinder = energyBlockFinder;
		else if(newWeapon instanceof MissileWeapon) blockFinder = missileBlockFinder;
		
		if(blockFinder != null) {
			AutoWeaponController controller = new AutoWeaponController(newWeapon, this, blockFinder);
			newWeapon.addController(controller);
			new AutoWeaponTPController(this, newWeapon, blockFinder, controller);
		}
	}
	
	public AutoWeaponShipProperties getAutoWeaponShipProps() { return (AutoWeaponShipProperties)settings; }
	
	@Override
	public float getFireRangeOf(Weapon weapon) {
		if(weapon instanceof EnergyWeapon) return getAutoWeaponShipProps().getEnergyRange();
		if(weapon instanceof MissileWeapon) return getAutoWeaponShipProps().getMissileRange();
		return Float.POSITIVE_INFINITY;
	}
	
	@Override
	public void destroy() {
		super.destroy();
		team.removeFrigate(this);
	}
	
	public void setTarget(TeamNode target) { currentTarget = target; }
	
	public TeamNode getCurrentTarget() { return currentTarget; }
	
	@Override
	public int getDestroyMoney() { return 500; }
	
	@Override
	public float getDefaultScale() { return 1.8f; }
	
	@Override
	protected ShipType getShipType() { return ShipType.Frigate; }
	
	@Override
	public Collection<Mission> getTargetMissions(Team team) {
		Collection<Mission> missions = new ArrayList<Mission>();
		Mission destroyMission = MissionFactory.createDestroyMission(team, this);
		missions.add(destroyMission);
		return missions;
	}
}